package com.resource.project.controller.system;

import com.resource.project.entity.system.Menu;
import com.resource.project.entity.RespBean;
import com.resource.project.entity.system.Role;
import com.resource.project.service.system.service.IMenuRoleService;
import com.resource.project.service.system.service.IMenuService;
import com.resource.project.service.system.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "权限管理")
@RequestMapping("/system/per/basic")
public class PerController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuRoleService menuRoleService;
    @ApiOperation(value = "查询所有角色")
    @GetMapping("/")
    public List<Role> getAllPostions(){
        return roleService.roles();
    }

    @ApiOperation(value = "查询菜单")
    @GetMapping("/menus")
    public List<Menu> getAllMenus(){
        return menuService.getAllMenuWithRole();
    }

    @ApiOperation(value = "根据角色查询菜单")
    @GetMapping("/{id}")
    public List<Long> getAllMenusByRole(@PathVariable Long id){
        return menuService.getAllMenuByRole(id);
    }

    @ApiOperation(value = "根据角色id修改菜单")
    @PutMapping("/")
    public RespBean updateMenuByRoleId(Long id, Long[] mids){
        if(menuRoleService.updateMenuByRoleId(id,mids)){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
}
